package tech.yangzhijie.basic.mp3;

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.CannotWriteException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3FileWriter;
import org.jaudiotagger.tag.*;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;

/**
 * @Author: 快乐在角落里
 * @Create: 2020/11/25 4:07
 */
public class MP3FileTest {


    @Test
    public void mp3Test(){
        File folder = new File("E:\\百度\\武林外传");
        File[] fileArr = folder.listFiles();
        for (File file : fileArr) {
            modifyMP3Files(file);
        }
    }


    private void modifyMP3Files(File targetFile){

        AudioFile audioFile = null;
        try {
            audioFile = AudioFileIO.read(targetFile);
        } catch (CannotReadException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TagException e) {
            e.printStackTrace();
        } catch (ReadOnlyFileException e) {
            e.printStackTrace();
        } catch (InvalidAudioFrameException e) {
            e.printStackTrace();
        }

        Tag tag = audioFile.getTag();
        String artist = tag.getFirst(FieldKey.ARTIST);//歌手
        String album = tag.getFirst(FieldKey.ALBUM);//专辑
        String songName = tag.getFirst(FieldKey.TITLE);//歌名
        String year = tag.getFirst(FieldKey.YEAR);//年份


//        System.out.println("文件名: " + targetFile.getName());
//        System.out.println("专辑: " + album);
//        System.out.println("歌手: " + artist);
//        System.out.println("歌名: " + songName);
//        System.out.println("年份: " + year);

        //String title = songName.replace("[武林外传].", "").replaceFirst("\\.", "-");
        String title = songName.replace("-", " ").replace(".", " ");
        File newFile = new File("E:\\音乐\\武林外传\\"+title+".mp3");
        updateTag(audioFile,title);
        targetFile.renameTo(newFile);
    }

    private void updateTag(AudioFile audioFile,String title){
        Tag tag = audioFile.getTag();
        try {
            //tag.setField(FieldKey.ARTIST,"海水不甜");//修改艺术家
            tag.setField(FieldKey.TITLE,title);//修改标题
            //tag.setField(FieldKey.ALBUM,"武林外传");//修改专辑
            //tag.setField(FieldKey.YEAR,"2006");//修改年份
            MP3FileWriter mp3FileWriter = new MP3FileWriter();
            mp3FileWriter.write(audioFile);
        } catch (FieldDataInvalidException e) {
            e.printStackTrace();
        } catch (CannotWriteException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test(){
        String str = "[武林外传].14.邢捕头勇破杀鸡案.燕小六力推防贼法";
        String replace = str.replace("[武林外传].", "").replaceFirst("\\.","-");

        System.out.println(replace);
    }
}
